#include "AppController.h"
#include <conprint.h>
#include "std.h"

AppController* AppController::mInstance = NULL;

void AppController::getUserInfo(){
	MAHandle myStore;
	MAHandle myData;
	MAHandle myRead;
	myStore = maOpenStore("setup", 0);
	if (myStore == STERR_NONEXISTENT){
		myStore = maOpenStore("setup", MAS_CREATE_IF_NECESSARY);
		mUserInfo = integerToString(maTime());
		myData = maCreatePlaceholder();
	    if(maCreateData(myData, mUserInfo.length()) == RES_OK)
		{
			maWriteData(myData, mUserInfo.c_str(), 0, mUserInfo.length());
			int result = maWriteStore(myStore, myData);
			if (result > 0){
				//lprintfln("write data successfully and close file");
				maCloseStore(myStore, 0);
				mNew = 1;
			}else{
				//lprintfln("write failure and delete file");
				maCloseStore(myStore, 1);
				mNew = -1;
			}
		}
	    maDestroyObject(myData);
	    return;
	}else{
		myRead = maCreatePlaceholder();
		int result = maReadStore(myStore, myRead);
		if (result > 0){
			maReadData(myRead, mUserInfo.pointer(), 0, maGetDataSize(myRead));
			//lprintfln("read file...");
			//lprintfln(mUserInfo.c_str());
			maCloseStore(myStore, 0);
			//lprintfln("close file");
			mNew = 0;
		}
		maDestroyObject(myRead);
		return;
	}
}

int AppController::getNew(){
	return mNew;
}

String& AppController::getUserInfoValue(){
	return mUserInfo;
}

void AppController::abortRegister(){
	MAHandle myStore;
	myStore = maOpenStore("setup", 0);
	if (myStore != STERR_NONEXISTENT){
		maCloseStore(myStore, 1);
	}
}

AppController::AppController() {
	getUserInfo();
	MAExtent screenSize = maGetScrSize();
	mScrWidth = EXTENT_X(screenSize);
	mScrHeight = EXTENT_Y(screenSize);
	mUrl = "http://xzone.vn:8001/luckyzonegetcontentv2.asmx?WSDL";
	mIsGetLRES = false;
	mDataTransfer = 0;
}

AppController::~AppController() {
	//Delete all the screens managed by the controller
	Vector_each(Screen*, itr, mScreens)
		delete *itr;

	//Delete all local data managed by the controller
	Vector_each(LRESItem, itr, mLRESList)
		delete itr;

	//Delete all the fonts managed by the controller
	for (HashMap<String, Font*>::Iterator itr = mFonts.begin(); itr
			!= mFonts.end(); itr++)
		delete itr->second;

	//Delete all the skins managed by the controller
	for (HashMap<String, WidgetSkin*>::Iterator itr = mSkins.begin(); itr
			!= mSkins.end(); itr++)
		delete itr->second;
}

//Instance manager
AppController* AppController::getInstance() {
	if (mInstance == NULL)
		mInstance = new AppController();

	return mInstance;
}

//Properties
void AppController::setMoblet(Moblet* moblet) {
	mMoblet = moblet;
}

Moblet* AppController::getMoblet() {
	return mMoblet;
}

//Registration
int AppController::registerScreen(Screen* screen) {
	mScreens.add(screen);

	return (int) mScreens.size() - 1;
}

void AppController::registerScreen(String screenName, Screen* screen) {
	mIndex.insert(screenName, registerScreen(screen));
}

//Show methods
void AppController::show(int screenIndex) {
	Screen* oldScreen = NULL;
	Screen* newScreen = NULL;

	if (mScreens[screenIndex] != NULL) {
		if (mHistory.size() > 0)
			oldScreen = getScreen(mHistory.peek());

		mHistory.push(screenIndex);
		newScreen = getScreen(screenIndex);

		newScreen->show();

		/*if(oldScreen != NULL)
		 ScreenTransition::makeTransition(oldScreen, newScreen, 1, 400, MOVEOVERTOP);
		 else
		 newScreen->show();*/
	}
}

void AppController::show(String screenName) {
	if (mIndex.find(screenName) != mIndex.end())
		show(mIndex.find(screenName)->second);
}

Screen* AppController::getScreen(int screenIndex) {
	if (screenIndex >= 0)
		return mScreens[screenIndex];
}

Screen* AppController::getScreen(String screenName) {
	Screen* tempScreen = NULL;
	if (mIndex.find(screenName) != mIndex.end())
		tempScreen = getScreen(mIndex.find(screenName)->second);

	return tempScreen;
}

//Move back one
void AppController::back() {
	if (mHistory.size() > 1) {
		int currentScreen = mHistory.peek();
		mHistory.pop();
		Screen* oldScreen = NULL;
		Screen* newScreen = NULL;

		if (mScreens[currentScreen] != NULL) {
			newScreen = getScreen(mHistory.peek());
			oldScreen = getScreen(currentScreen);

			newScreen->show();

			/*if(oldScreen != NULL)
			 ScreenTransition::makeTransition(oldScreen, newScreen, -1, 400, REVEALFROMBELOW);
			 else
			 newScreen->show();*/
		}
	}
}

//Close the application
void AppController::close() {
	if (mMoblet != NULL)
		mMoblet->close();
}

int AppController::getIndex(String& screenName) {
	return mIndex[screenName];
}

void AppController::addFont(String name, Font* font) {
	mFonts.insert(name, font);
}

Font* AppController::getFont(String name) {
	return mFonts.find(name)->second;
}

void AppController::addSkin(String name, WidgetSkin* skin) {
	mSkins.insert(name, skin);
}

WidgetSkin* AppController::getSkin(String name) {
	return mSkins.find(name)->second;
}

int AppController::getScreenWidth() {
	return mScrWidth;
}

int AppController::getScreenHeight() {
	return mScrHeight;
}

int AppController::getDataTransfer() {
	return mDataTransfer;
}

void AppController::setDataTransfer(int input) {
	mDataTransfer += input;
}

XmlConnection* AppController::getConnection() {
	if (mConn == NULL)
		mConn = new XmlConnection();

	return mConn;
}

void AppController::setCompanyId(String& value) {
	mCompanyId = value;
}

String& AppController::getCompanyId() {
	return mCompanyId;
}

String& AppController::getUrl() {
	return mUrl;
}
